#include "stm32f10x.h"  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Motor.h"
#include "MatrixKey.h"
#include "RED.h"
#include "DHT11.h"
#include <string.h>
#include "Timer.h"
#include "beep.h"
#include "Serial.h"
#include "Buzzer.h"
#include <stdio.h>


char RxData;					//接收蓝牙数据

uint8_t KeyNum,RED_Data,RH,TH;		//分别用于获取按键，红外，温度，湿度
int8_t Speed;		//风扇速度
uint8_t Key_M = 1,Time_demo;		//用于切换模式
uint8_t Time_1,Time_10,Time;		//个位和十位的时间,总时间
uint8_t temperature = 30;	//阈值设置
int8_t dang = 0;			//档数
uint8_t Num = 1,Timedemo;	//标志位

uint8_t BluetoothKey = 0;  // 存储蓝牙接收到的按键值
uint8_t RxBuffer[10];    // 接收缓冲区
uint8_t RxIndex = 0;     // 缓冲区索引
uint8_t RxFlag = 0;      // 接收完成标志

uint8_t beepCounter = 0; 

char BlueToothSendBuf[32];

int main(void)
{
	/*模块初始化*/
	OLED_Init();		//OLED初始化
	Motor_Init();
	DHT11_Init();
	MatrixKey_Init();
	Red_Init();
	Timer_Init();
	Serial_Init();
	Buzzer_Init();

	BEEP_GPIO_Config();
	
	OLED_ShowChinese(0,0,"温度");
	OLED_ShowChinese(16*4,0,"湿度");
	
	OLED_ShowChinese(0,16,"模式");
	OLED_ShowChinese(16*4,16,"档数");
	OLED_ShowNum(16*2,16,Key_M,1,OLED_8X16);
	OLED_ShowSignedNum(16*6,16*1,dang,2,OLED_8X16);
	
	OLED_ShowChinese(0,16*2,"速度：");
	OLED_ShowSignedNum(16*3,16*2,Speed,3,OLED_8X16);
	
	OLED_ShowChinese(0,16*3,"倒计时：");
	OLED_ShowNum(16*4,16*3,Time,2,OLED_8X16);
	OLED_Update();
	
	GPIO_SetBits(GPIOB, GPIO_Pin_0);
	GPIO_SetBits(GPIOB, GPIO_Pin_1);
	Delay_ms(100);
	GPIO_ResetBits(GPIOB, GPIO_Pin_0);
	GPIO_ResetBits(GPIOB, GPIO_Pin_1);

	while (1)
	{
		DHT11_Read_Data(&TH,&RH);//获取当前温湿度
		OLED_ShowNum(16*2,0,TH,2,OLED_8X16);
		OLED_ShowNum(16*6,0,RH,2,OLED_8X16);
		OLED_Update();
		Buzzer_OFF();
		
		if(Timedemo == 1)
		{
			if(RED_Data == 1)	//如果有人
			{
				OLED_ShowString(16*6,16*2,"ON ",OLED_8X16);
				if(temperature < TH)	//实际温度高于设置阈值
				{
					Speed = (TH-temperature)*10;	
					if(Speed > 100)
					{
						Speed = 100;
					}
				}
				else Speed = 0;			
			}
			else
			{
				OLED_ShowString(16*6,16*2,"OFF ",OLED_8X16);
				Speed = 0;
			}
			OLED_ShowSignedNum(16*3,16*2,Speed,3,OLED_8X16);			
			
			OLED_Update();
			Motor_SetSpeed(Speed);
		}
		Delay_ms(500);

	}
}


uint16_t count;
uint8_t yy;

void TIM1_UP_IRQHandler(void)		//每1毫秒进一次中断
{
	if (TIM_GetITStatus(TIM1, TIM_IT_Update) == SET)
	{
		count++;
		if(count == 1000)
		{
			count = 0;
			if(Time_demo == 0 && Time > 0)	//进入倒计时模式
			{
				Time--;
				if(Time == 0)		//倒计时结束
				{
					Timedemo = 0;	//关闭自动挡
					Speed = 0;		//速度置0
					dang = 0;		//去除记录
					Time_1 = 0;
					Time_10 = 0;
					Motor_SetSpeed(0);
					OLED_ShowSignedNum(16*3,16*2,Speed,3,OLED_8X16);			
					OLED_ShowSignedNum(16*6,16*1,dang,2,OLED_8X16);
				}
				OLED_ShowNum(16*4,16*3,Time,2,OLED_8X16);
				OLED_Update();
				
			}
		}
		if(count%20 == 0)	//获取按键值
		{
			RED_Data = Red_Rdata();		//检测是否有人
			// 优先处理蓝牙按键
			if(BluetoothKey != 0)
			{
				KeyNum = BluetoothKey;  // 将蓝牙按键值赋给KeyNum
				BluetoothKey = 0;       // 重置蓝牙按键
			}
			else
			{
			  if(RxFlag) 
				{
					if(Key_M == 1) { 

                // 将接收到的字符串转换为整数
                int speed_val = atoi((char*)RxBuffer);
                
                // 限幅处理
                if(speed_val > 100) speed_val = 100;
                if(speed_val < -100) speed_val = -100;
						     Buzzer_ON();
                 beepCounter = 30;
                
						
                Motor_SetSpeed(speed_val);
				        dang=0;
                OLED_ShowSignedNum(16*3,16*2,speed_val,3,OLED_8X16);
				        OLED_ShowSignedNum(16*6,16*1,dang,2,OLED_8X16);
                OLED_Update();
					}
                RxFlag = 0; // 清除标志
			}
       else
			{
				KeyNum = Key_LoopY();
			}
		}
			if(KeyNum)		//如果按键按下
			{
				Buzzer_ON();        // 打开蜂鸣器
        beepCounter = 30;   // 设置30ms计时
				if(KeyNum == 3)				//切换模式键 Key_M = 1:手动，Key_M = 2:自动
				{
					Num = 1;		//手动自动模式
					Key_M += 1;
					if(Key_M > 2){Key_M = 1;}
					
					OLED_ShowNum(16*2,16,Key_M,1,OLED_8X16);
					OLED_Update();
				}
				if(Num == 1)		//手动自动模式
				{
					if(Key_M == 1)			//模式1手动挡模式
					{
						Timedemo = 0;
						OLED_ClearArea(16*3,16,16*8,16);
						OLED_ShowChinese(16*4,16,"档数");
						
						if (KeyNum == 1)	//减档
						{
							dang--;
							if(dang < (-5))
							{
								Motor_SetSpeed(0);
								Delay_ms(500);
								dang = -5;
							}
						}
						if (KeyNum == 2)	//加档
						{
							dang++;
							if(dang > 5)
							{
								Motor_SetSpeed(0);
								Delay_ms(500);
								dang = 5;
							}
						}
						Speed = dang*20;
						OLED_ShowString(16*6,16*2,"    ",OLED_8X16);
						OLED_ShowSignedNum(16*6,16*1,dang,2,OLED_8X16);
						OLED_ShowSignedNum(16*3,16*2,Speed,3,OLED_8X16);			
						
						OLED_Update();
						Motor_SetSpeed(Speed);

					}
					if(Key_M == 2)
					{
						Timedemo = 1;
						dang = 0;
						OLED_ClearArea(16*3,16,16*8,16);
						OLED_ShowChinese(16*4,16,"阈值");

						if (KeyNum == 1)	//温度阈值--
						{
							temperature--;
							if(temperature < 16)
							{
								temperature = 16;
							}
						}
						if (KeyNum == 2)	//温度阈值++
						{
							temperature++;
							if(temperature > 40)
							{
								temperature = 40;
							}
						}
						OLED_ShowNum(16*6,16*1,temperature,2,OLED_8X16);
						OLED_Update();
					}
				}
				if(KeyNum == 4)		//切换倒计时模式
				{
					Num = 2;
					Time_demo++;
					if(Time_demo == 2)	//Time_demo=2相当于确定键
					{
						Num = 1;
						Time_demo = 0;
						OLED_ShowString(16*5+8,16*3,"  ",OLED_8X16);
						
						OLED_Update();
					}
				}
				if(Num == 2)
				{
					if(Time_demo == 1)	//输入需要倒计时的时间
					{
						if(KeyNum == 1)
						{
							Time_10++;
							if(Time_10 > 9)
							{
								Time_10 = 0;
							}
						}
						if(KeyNum == 2)
						{
							Time_1++;
							if(Time_1 > 9)
							{
								Time_1 = 0;
							}
						}
						Time = Time_10*10 + Time_1;
						OLED_ShowNum(16*4,16*3,Time,2,OLED_8X16);
						OLED_ShowString(16*5+8,16*3,"<-",OLED_8X16);
						
						OLED_Update();
					}
				}
			}
		
		 }
		if (beepCounter > 0) {
            beepCounter--;
            if (beepCounter == 0) 
							{
                Buzzer_OFF();  // 计时结束关闭蜂鸣器
            }
        }
		
		TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
	}
}
uint8_t RxData_demo,RxData_de,RxState;

void USART1_IRQHandler(void)
{

  if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
    {
        uint8_t temp = USART_ReceiveData(USART1);
			// 新增蓝牙字符命令处理
		switch(temp) 
			{
			case 'A': // 切换模式
				BluetoothKey = 3;
				break;
			case 'B': // 加
				BluetoothKey = 2;
				break;
			case 'C': // 减
				BluetoothKey = 1;
				break;
			case 'D': // 定时器模式
				BluetoothKey = 4;
				break;
			case 'E': 
                DHT11_Read_Data(&TH, &RH);
                sprintf(BlueToothSendBuf, "Temp:%dC Hum:%d%%\r\n", TH, RH);
                Serial_SendString(BlueToothSendBuf);
        break;
        
			default:
        if(temp == '\r' || temp == '\n') 
					{
            RxBuffer[RxIndex] = '\0'; // 添加字符串结束符
            RxFlag = 1;               // 设置接收完成标志
            RxIndex = 0;               // 重置索引
        } 
        else if (RxIndex < sizeof(RxBuffer)-1) 
					{
            RxBuffer[RxIndex++] = temp; // 存储数据
        }
        break;
      
    }
			USART_ClearITPendingBit(USART1, USART_IT_RXNE);
  }
}


